<template>
  <div v-show="dialogValue" class="add-form f-c">
    <div class="header">
      <div class="title">
        添加/编辑用户
      </div>
      <el-button @click="onPageBack">返回</el-button>
    </div>
    <div class="add-form-content f-g">
      <SubmitForm
        :rule-form="ruleForm"
        :rules="rules"
        :form-list="formList"
        @close="onPageBack"
        @save="onSave"
      >
        <el-button v-if="this.ruleForm.id" style="position:absolute;top: 206px" type="primary" @click="editPassword">修改密码</el-button>
      </SubmitForm>
    </div>
    <el-dialog
      title="修改密码"
      :visible.sync="dialogValue2"
      :close-on-click-modal="false"
      width="600px"
      append-to-body
    >
      <SubmitForm
        :rule-form="ruleForm2"
        :rules="rules2"
        :form-list="formList2"
        @close="dialogValue2 = false"
        @save="onSavePassword"
      />
    </el-dialog>
  </div>
</template>

<script>
import { http } from '@/api'

export default {
  name: 'FormAdd',
  data() {
    return {
      dialogValue: false,
      ruleForm: {
      },
      rules: {
        username: [{ required: true, message: '不能为空' }],
        password: [{ required: true, message: '不能为空' }],
        realname: [{ required: true, message: '不能为空' }],
        phone: [{ required: true, message: '不能为空' }]
      },
      formList: [
        {
          key: 'realname',
          name: 'input',
          label: '用户姓名',
          colSpan: 12
        },
        {
          key: 'departId',
          name: 'select',
          label: '用户单位',
          colSpan: 12,
          options: [

          ]
        },
        {
          key: 'accountType',
          name: 'radio',
          label: '账号类型',
          colSpan: 24,
          defaultValue: '0',
          options: [
            { value: '0', name: '临时账号' },
            { value: '1', name: '管理员' }
          ]
        },
        {
          key: 'username',
          name: 'input',
          label: '用户账号',
          colSpan: 12,
          disabled: false
        },
        {
          key: 'password',
          name: 'input',
          label: '密码',
          colSpan: 12,
          disabled: false
        },
        {
          key: 'phone',
          name: 'input',
          label: '联系方式',
          colSpan: 12
        },
        {
          key: 'status',
          name: 'switch',
          label: '是否启用',
          colSpan: 24,
          defaultValue: '0',
          activeValue: '0',
          inactiveValue: '1'
        }
      ],
      dialogValue2: false,

      ruleForm2: {
        username: '',
        password: '',
        password2: ''
      },
      rules2: {
        username: [{ required: true, message: '不能为空' }],
        password: [{ required: true, message: '不能为空' }],
        password2: [{ required: true, message: '不能为空' }]
      },
      formList2: [
        {
          key: 'username',
          name: 'input',
          label: '用户账号',
          colSpan: 24,
          disabled: true
        },
        {
          key: 'password',
          name: 'input',
          label: '密码',
          type: 'password',
          colSpan: 24,
          disabled: false
        },
        {
          key: 'password2',
          name: 'input',
          label: '确认新密码',
          type: 'password',
          colSpan: 24,
          disabled: false
        }
      ]
    }
  },
  created() {
    this.getlist()
  },
  methods: {
    getlist() {

    },
    getForm() {
      const form = {
        id: undefined
      }
      this.formList.forEach(el => {
        form[el.key] = el.defaultValue || ''
      })
      return form
    },
    async open(item) {
      this.ruleForm = this.getForm()
      this.formList[3].disabled = false
      this.formList[4].disabled = false
      this.rules.username[0].required = true
      this.rules.password[0].required = true
      if (item) {
        for (const key in this.ruleForm) {
          this.ruleForm[key] = item[key]
        }
        this.formList[3].disabled = true
        this.formList[4].disabled = true
        this.rules.username[0].required = false
        this.rules.password[0].required = false
      }
      http.dept.list().then(res => {
        res.data.forEach(element => {
          element.name = element.departName
          element.value = element.id
        });
        this.formList[1].options = res.data
      })
      this.dialogValue = true
    },
    onPageBack() {
      this.$emit('get-page')
      this.dialogValue = false
    },
    onSave() {
      this.formList[1].options.forEach(element => {
        if (element.id == this.ruleForm.departId) {
          this.ruleForm.orgCode = element.code
        }
      });
      http.user[this.ruleForm.id ? 'update' : 'create']({ ...this.ruleForm }).then(res => {
        this.$message.success(res.msg)
        this.onPageBack()
      })
    },
    editPassword() {
      this.dialogValue2 = true
      this.ruleForm2.username = this.ruleForm.username
    },
    onSavePassword() {
      if (this.ruleForm2.password !== this.ruleForm2.password2) {
        return this.$message.warning('两次输入密码不一致')
      }
      http.user.updatePassword({
        id: this.ruleForm.id,
        username: this.ruleForm.username,
        password: this.ruleForm2.password,
        confirmPassword: this.ruleForm2.password2
      }).then(res => {
        this.$message.success(res.msg)
        this.ruleForm2.password = ''
        this.ruleForm2.password2 = ''
        this.dialogValue2 = false
      })
    }
  }
}
</script>

<style scoped lang="scss">
</style>
